class Msg {
    constructor() {
    }

    public static webSocket:WebSocket;

    public static connect():void{
        Msg.webSocket = new WebSocket("ws://");
        Msg.webSocket.onclose = Msg.onClose;
        Msg.webSocket.onopen = Msg.onOpen;
        Msg.webSocket.onmessage = Msg.onMessage;
        Msg.webSocket.onerror = Msg.onError;
    }

    private static onClose(evt):void{
        alert("游戏断开连接!")
    }

    private static onOpen():void{

    }

    private static onMessage(evt):void{
        // 0加入房间  1心跳 2聊天消息
        var msg:Array<string> = evt.data.split("=");
        if(msg[0] == "0"){
            Msg.GC_JOIN(msg[1]);
        }else if(msg[0] == "1"){
            Msg.GC_HEART();
        }else if(msg[0] == "2"){
            Msg.GC_CHAT(msg[1]);
        }
    }


    private static onError(evt):void{

    }

    /**
     * 心跳
     * @constructor
     */
    public static CG_HEART():void{
        Msg.webSocket.send("1={}");
    }

    /**
     * 聊天
     * @param {string} msg
     * @constructor
     */
    public static CG_CHAT(msg:string):void{
        Msg.webSocket.send("2={}");
    }

    /**
     * 加入聊天房间
     * @param {string} msg
     * @constructor
     */
    public static CG_JOIN(msg:string):void{
        Msg.webSocket.send("1={}");
    }


    /**
     * 心跳返回
     * @constructor
     */
    public static GC_HEART():void{
    }

    /***
     * 聊天返回
     * @param {string} msg
     * @constructor
     */
    public static GC_CHAT(msg:string):void{
        var data =  JSON.stringify(msg);
    }

    /**
     * 加入游戏聊天房间返回
     * @param {string} msg
     * @constructor
     */
    public static GC_JOIN(msg:string):void{
        //显示主页面
        ScreenManager.showMainPage();

        var data =  JSON.stringify(msg);
    }
}